using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.Web.BL.CommonPage;
using NTTHAO.CommonWeb;

public partial class admin_webparts_Plug_ins_CommonPage_CommonMoveItem : System.Web.UI.UserControl
{
    public string _Type = String.Empty;
    public string _ID = "";
    public string _Title = string.Empty;
    public string _ErrorMsg = "";
    public string _ReturnUrl = "/admin/CommonPageContent.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            this.btnSave.Text = LanguageUtility.GetLanguageText("SAVE", Session);

            string action = Request.QueryString["action"].Trim();
            string idStr = Request.QueryString["id"].Trim();

            if (Request.QueryString["ReturnUrl"] != null)
            {
                _ReturnUrl = Request.QueryString["ReturnUrl"];
            }
            else if ("movemenu".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
            {
                _ReturnUrl += "?action=view&g=" + Request.QueryString["g"] + "&mnuid=" + Request.QueryString["id"] + "&time=" + DateTime.Now.Ticks.ToString();
            }
            else 
            {
                _ReturnUrl += "?action=view&g=" + Request.QueryString["g"] + "&mnuid=" + Request.QueryString["mnuid"] + "&time=" + DateTime.Now.Ticks.ToString();
            }

            if (!IsPostBack)
            {
                if ("movemenu".Equals(action))
                {
                    CommonMenu mnu = new CommonMenu(Int32.Parse(idStr));
                    if (mnu.ID > 0)
                    {
                        txtID.Text = _ID = mnu.ID.ToString();
                        txtName.Text = _Title = mnu.Text;
                        _Type = LanguageUtility.GetLanguageText("MENU", Session);
                    }
                }
                else if ("movecontent".Equals(action))
                {
                    CommonPageContent content = new CommonPageContent(Int32.Parse(idStr));
                    if (content.ID > 0)
                    {
                        txtID.Text = _ID = content.ID.ToString();
                        txtName.Text = _Title = content.Header;
                        _Type = LanguageUtility.GetLanguageText("CONTENT", Session);
                    }
                }
                else if ("movealbum".Equals(action))
                {
                    CommonAlbum album = new CommonAlbum(Int32.Parse(idStr));
                    if (album.ID > 0)
                    {
                        txtID.Text = _ID = album.ID.ToString();
                        txtName.Text = _Title = album.AlbumName;
                        _Type = LanguageUtility.GetLanguageText("IMAGE_ALBUM", Session);
                    }
                }
            }
        }
        catch (Exception)
        {
        }
    }

    private void MoveMenu(int menuID, int toParentID, bool recursive)
    {
        CommonMenu mnu = new CommonMenu(menuID);
        mnu.ParentID = toParentID;
        mnu.Update();

        if (recursive)
        {
            DataSet ds = mnu.GetAllByParent(mnu.ID);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    int id = (int)row[CommonMenu.ID__COLUMN_NAME];
                    this.MoveMenu(id, mnu.ID, recursive);
                }
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            string action = Request.QueryString["action"].Trim();
            string idStr = Request.QueryString["id"].Trim();

            if ("movemenu".Equals(action))
            {
                this.MoveMenu(Int32.Parse(idStr), Int32.Parse(txtToID.Text.Trim()), true);

                Response.Redirect("CommonPageContent.aspx?action=reloadtop", false);
                return;
            }
            else if ("movecontent".Equals(action))
            {
                CommonPageContent content = new CommonPageContent(Int32.Parse(idStr));
                content.MenuID = Int32.Parse(txtToID.Text.Trim());
                content.Update();

                Response.Redirect("CommonPageContent.aspx?action=view&g=" + Request.QueryString["g"] + "&mnuid=" + Request.QueryString["mnuid"], false);
                return;
            }
            else if ("movealbum".Equals(action))
            {
                CommonAlbum album = new CommonAlbum(Int32.Parse(idStr));
                album.MenuID = Int32.Parse(txtToID.Text.Trim());
                album.Update();

                Response.Redirect("CommonPageContent.aspx?action=view&g=" + Request.QueryString["g"] + "&mnuid=" + Request.QueryString["mnuid"], false);
                return;
            }
        }
        catch (Exception ex)
        {
            this._ErrorMsg = ex.Message + " txtToID=" + txtToID.Text;
        }
    }
}
